package com.example.lanou.mynews.fragment.news;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.lanou.mynews.R;
import com.example.lanou.mynews.adapter.NormalFragAdapter;
import com.example.lanou.mynews.adapter.ScrollPagerHeadAdapter;
import com.example.lanou.mynews.application.MyApplication;
import com.example.lanou.mynews.beans.Adsdb;
import com.example.lanou.mynews.beans.AllDataBeans;
import com.example.lanou.mynews.beans.PerDataBeans;
import com.example.lanou.mynews.face.URL;
import com.example.lanou.mynews.thread.AllDataBeansThread;
import com.example.lanou.mynews.thread.HeadViewThread;

import java.lang.reflect.Array;
import java.util.ArrayList;


/**
 * handler :  msg.what==0 is for headview
 * msg.what==1 is for this page ,get data
 * msg.what==3 is request error
 * <p/>
 * <p/>
 * Created by dllo on 16/1/12.
 */
public class NormalFrag extends Fragment implements ViewPager.OnPageChangeListener, View.OnTouchListener, URL {
    private ScrollPagerHeadAdapter headAdapter;
    private ViewPager viewPager;
    private ListView listView;
    private Handler handler;
    private NormalFragAdapter listAdapter;
    private View headView;
    private int[] photoAuto = {0};
    private AllDataBeans allData;
    private HeadViewThread thread;
    private int[] views = {R.id.item_head_view1, R.id.item_head_view2, R.id.item_head_view3, R.id.item_head_view4};
    private AllDataBeansThread dataBeansThread;
    private int size;//头布局图片个数
    private String tid;

    public NormalFrag(String tid) {
        handlerRequest();
        this.tid= tid;
    }

    /***
     * for my handler request
     */
    public void handlerRequest() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0) {
                    viewPager.setCurrentItem(photoAuto[0] + 1);
                }
                if(msg.what==1){
                    allData = (AllDataBeans) msg.obj;
                    setAllData();
                    if(!(null==allData.getTopic().get(0).getAds())){
                        getHead();
                    thread = new HeadViewThread(handler);
                    thread.start();
                    }
                }
                if(msg.what==3)
                    //TODO  进行网络获取失败的处理
                    ;
            }
        };
    }
    public void setAllData(){
        listAdapter.setData(allData.getTopic());
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.news_frag_per_normal, container, false);
        listView = (ListView) view.findViewById(R.id.news_per_frag_lv);
        dataBeansThread = new AllDataBeansThread(tid, "0-20", handler);
        dataBeansThread.start();
        listAdapter = new NormalFragAdapter();
        listView.setAdapter(listAdapter);
        return view;
    }

    /***
     * can get zhe size of viewpager, and set the point del
     */
    public void getHead() {
        headAdapter = new ScrollPagerHeadAdapter(getImgUrl());
        headView = View.inflate(MyApplication.getContext(), R.layout.headview1, null);
        headView.findViewById(views[0]).setSelected(true);
        size=allData.getTopic().get(0).getAds().size();
        for (int i = views.length-1; i > size; i--) {
            headView.findViewById(views[i]).setVisibility(View.GONE);
        }
        viewPager = (ViewPager) headView.findViewById(R.id.item_head_vp);
        viewPager.addOnPageChangeListener(this);
        viewPager.setAdapter(headAdapter);
        viewPager.setPageMargin(10);
        viewPager.setOnTouchListener(this);
        listView.addHeaderView(headView);
        viewPager.setCurrentItem(photoAuto[0]);
    }

    /***
     * for the headview  imageUrl;
     * @return
     */
    public ArrayList<String> getImgUrl(){
        ArrayList<String> data = new ArrayList<>();
        for (Adsdb adsdb : allData.getTopic().get(0).getAds()) {
            data.add(adsdb.getImgsrc());
        }
        return data;
    }


    //轮播图的ontouch 事件
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                thread.setFlag(false);
                break;
            case MotionEvent.ACTION_UP:
                thread = new HeadViewThread(handler);
                thread.start();
                break;
        }

        return false;
    }

    /***
     * just let the headview restart
     */
    @Override
    public void onResume() {
        super.onResume();
        photoAuto[0] = Integer.MAX_VALUE / 2 - Integer.MAX_VALUE % 4;


        Log.i("1111111111", "resume");

    }

    /***
     * headview stop
     *
     */
    @Override
    public void onStop() {
        super.onStop();
        if(null!=thread)
        thread.setFlag(false);
        Log.i("1111111", "stop");
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    /***
     * headview
     *
     * the event of pageselected
     * @param position
     */
    @Override
    public void onPageSelected(int position) {
        photoAuto[0] = position;
        setTextForHead(position);
        ImageView iv = (ImageView) headView.findViewById(views[position % 4]);
        removeSelectAll();
        iv.setSelected(true);
        Log.d("NormalFrag", "!!!!!!!!!!!position:" + position);
    }

    /***
     * set the title text
     * @param position
     */
    public void setTextForHead(int position){
       TextView textView= (TextView) headView.findViewById(R.id.item_head_title_tv);
        textView.setText(allData.getTopic().get(0).getAds().get(position%size).getTitle());
    }

    /***
     * let the point disselected
     */
    private void removeSelectAll() {
        for (int i = 0; i < views.length; i++) {
            ImageView iv = (ImageView) headView.findViewById(views[i]);
            iv.setSelected(false);


        }
    }


    @Override
    public void onPageScrollStateChanged(int state) {

    }


}
